/**
 *  File: SendRePayService.java
 *  Date         Author      Changes
 *  2016-10-11    wangtq       Create
 *
 */
package com.kunpu.mami.channel.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.kunpu.mami.channel.common.ResConstants;
import com.kunpu.mami.channel.domain.TransReqBF0040001;
import com.kunpu.mami.channel.exception.MamiiException;

/**
 * 宝付提现接口服务
 * @author wangtq
 * @date 2016-10-11
 * @version 0.1
 *
 */
@Service
public class SendRePayService extends BaseSendRepayService{
	
	private final Logger       logger = LoggerFactory.getLogger(this.getClass());
	
    /**
     *	提现
     * @param trans_id  订单
     * @param txn_amt   提现金额
     * @param userName  用户名称
     * @param accNo     银行卡号
     * @param banckname 银行名称 
     * @return
     */
	public void sendRePay(String transId , String txnAmt,String userName,String accNo,String bankName ){
		logger.info("==================== 渠道service sendRePay 提现 start ============= \n 入参trans_id{},txnAmt{},userName{},accNo{},bankName{}:",transId,txnAmt,userName,accNo,bankName);
		try{
			TransReqBF0040001 transReqData = new TransReqBF0040001();
	        transReqData.setTrans_no(transId);
	        transReqData.setTrans_money(txnAmt);
	        transReqData.setTo_acc_name(userName);
	        transReqData.setTo_acc_no(accNo);
	        transReqData.setTo_bank_name(bankName);
	        transReqData.setTo_pro_name("");
	        transReqData.setTo_city_name("");
	        transReqData.setTo_acc_dept("");
	        transReqData.setTrans_summary("gs"+new Date());
	        rePayService(transReqData);
			
		}catch(MamiiException me){
			logger.error("==================== sendRePay 提现异常，异常信息为：",me.getMessage(),me);
			throw new MamiiException(me.getErrorCode() , me.getMessage());
		}catch(Exception e){
			logger.error("==================== sendRePay 提现时异常，异常信息为：",e.getMessage(),e);
			throw new MamiiException(ResConstants.PAY_CASH_EXCEPTION.getCode() , "提现时异常");
		}
		logger.info("===================== 渠道service sendRePay 提现 end ====================");
	}
	
	
	/**
	 * 代收类交易
	 * @param trans_id 商户订单号
	 * @param bind_id 绑定标识号
	 * @param txn_amt 交易金额
	 * @param sms_code 短信验证码
	 * @param additional_info
	 * @param req_reserved
	 * @return
	 */
	private  String buildSendReChargeParam(String trans_id, String bind_id, String txn_amt, String sms_code, String additional_info, String req_reserved) {
		String trade_date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String txn_sub_type = "04";// 交易子类
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		sb.append("\"txn_sub_type\":\"" + txn_sub_type + "\"");
		sb.append(",\"biz_type\":\"0000\"");// 接入类型默认0000
		sb.append(",\"terminal_id\":\"" + terminal_id + "\"");
		sb.append(",\"member_id\":\"" + member_id + "\"");
		sb.append(",\"trans_serial_no\":\"" + member_id + "" + trade_date + "\"");
		sb.append(",\"trans_id\":\"" + trans_id + "\"");
		sb.append(",\"bind_id\":\"" + bind_id + "\"");
		sb.append(",\"txn_amt\":\"" + txn_amt + "\"");
		sb.append(",\"sms_code\":\"" + sms_code + "\"");
		sb.append(",\"trade_date\":\"" + trade_date + "\"");
		sb.append(",\"additional_info\":\"" + additional_info + "\"");
		sb.append(",\"req_reserved\":\"" + req_reserved + "\"");
		sb.append("}");
		return sb.toString();
	}
		

	

	
}
